package com.xcity.db.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.xcity.game.employee.template.EmployeeTemplate;

import naga.x.App;
import naga.x.game.template.TemplateService;

public class EmployeeTemplateTypeHandler extends BaseTypeHandler<EmployeeTemplate> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, EmployeeTemplate parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setInt(i, parameter.getId());
	}

	@Override
	public EmployeeTemplate getNullableResult(ResultSet rs, String columnName) throws SQLException {
		return findTemplate(rs.getInt(columnName));
	}

	@Override
	public EmployeeTemplate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		return findTemplate(rs.getInt(columnIndex));
	}

	@Override
	public EmployeeTemplate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		return findTemplate(cs.getInt(columnIndex));
	}
	
	private static EmployeeTemplate findTemplate(int id) {
		return App.getApp().getServiceManager().get(TemplateService.class).get(EmployeeTemplate.class, id);
	}

}
